package com.lottery.service.pc28.lobbyService.impl;

import com.alibaba.fastjson.JSON;
import com.lottery.dao.pc28.lobbyDao.*;
import com.lottery.dto.MyOrderDto;
import com.lottery.dto.WinningResultsDto;
import com.lottery.entity.Bettinginfo;
import com.lottery.entity.Lottery;
import com.lottery.service.pc28.lobbyService.MyOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MyOrderServiceImpl implements MyOrderService {

	@Autowired
	private BettinginfoDao bettinginfoDao;

	@Autowired
	private LotteryExchangeDao lotteryExchangeDao;

	@Autowired
	private LotteryDao lotteryDao;

	@Autowired
	private WinningResultsDao winningResultsDao;

	@Autowired
	private MyOrderDao myOrderDao;

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 查询我的订单
	 */
	@Override
	// public String selectMyOrderByUserId(int userId, String expect, int
	// pageSize, int pageNow, int lotteyType) {
	public String selectMyOrderByUserId(int userId, int pageSize, int pageNow, int lotteyType) {

		Map<String, Object> maxMap = new HashMap<String, Object>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {

			int startPage = (pageNow - 1) * pageSize;
			List<MyOrderDto> myOrdeList = myOrderDao.selectMyOrderByUserId(userId, pageSize, startPage, lotteyType);

			// List<MyOrderDto> myOrdeList =
			// myOrderDao.selectMyOrderByUserId(userId, expect, pageSize,
			// startPage,
			// lotteyType);
			String expect = null;
			for (MyOrderDto myOrde : myOrdeList) {

				Map<String, Object> map = new HashMap<String, Object>();
				// // 期号
				expect = myOrde.getExpect();
				// 彩票类型
				Integer type = myOrde.getLotteyType();
				// 投注
				double amountMoney = myOrde.getAmountMoney();
				// 下注时间
				Date bettingDate = myOrde.getBettingDate();
				SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
				String format = sdf.format(bettingDate);
				// 分割时间月和天
				String[] split = format.split("/");

				String month = split[0];
				String day = split[1];

				map.put("month", month);
				map.put("day", day);
				map.put("expect", expect);
				map.put("lotteyType", type);
				map.put("amountMoney", amountMoney);

				// 查询中奖状态
				List<Bettinginfo> bettinginfoList = bettinginfoDao.selectBettinginfoByExpect(userId, expect, type);
				int state = 0;
				for (Bettinginfo bettinginfo : bettinginfoList) {
					state = bettinginfo.getState();
					if (1 == state) {
						state = 1;
						break;
					}
				}
				map.put("state", state);

				if (null != map && map.size() > 0) {
					list.add(map);
				}
			}
			maxMap.put("array", list);
			maxMap.put("result", "0");
			maxMap.put("msg", "成功");
		} catch (Exception e) {
			logger.error("selectMyOrderByUserId error:", e);
			maxMap.put("result", "1");
			maxMap.put("msg", "查询订单异常");
		}

		return JSON.toJSONString(maxMap, true);
	}

	/**
	 * 订单详情
	 */
	@Override
	public String orderDetails(int userId, String expect, int pageSize, int pageNow) {

		Map<String, Object> maxMap = new HashMap<String, Object>();
		try {
			// 获取用户该期的投注总金额
			Double betTotalAmount = bettinginfoDao.betTotalAmount(userId, expect);
			// 获取用户该期的中奖总金额
			Double winTotalAmount = lotteryExchangeDao.winTotalAmount(userId, expect);
			// 获取中奖号码
			String opencode = null;
			Lottery selectLotteryByExpect = lotteryDao.selectLotteryByExpect(expect);
			if (selectLotteryByExpect != null) {
				String opencode1 = selectLotteryByExpect.getOpencode();
				opencode = opencode1.replace(",", " ");
			}
			// 下注总条数
			int selectWinningResultsCount = winningResultsDao.selectWinningResultsCount(userId, expect);
			// 获取选号详情
			int startPage = (pageNow - 1) * pageSize;
			if (startPage > selectWinningResultsCount) {
				maxMap.put("result", "1");
				maxMap.put("msg", "没有更多记录！");
				return JSON.toJSONString(maxMap, true);
			}
			List<WinningResultsDto> winningResults = winningResultsDao.selectWinningResults(userId, expect, startPage,
					pageSize);
			if (winningResults.size() == 0) {
				maxMap.put("array", winningResults);
				maxMap.put("result", "0");
				maxMap.put("msg", "查询成功！");

				return JSON.toJSONString(maxMap, true);
			}
			// 下注时间
			Date bettingDate1 = winningResults.get(0).getBettingDate();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String bettingDate = sdf.format(bettingDate1);

			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			int state1 = 0;
			for (WinningResultsDto winningResultsDto : winningResults) {
				Map<String, Object> map = new HashMap<String, Object>();
				String bettingrule = winningResultsDto.getBettingrule() + " " + winningResultsDto.getBettingMoney();
				int state = winningResultsDto.getState();
				double winningamount = winningResultsDto.getWinningamount();
				DecimalFormat df = new DecimalFormat("#.##");
				String get = df.format(winningamount);
				// 获取投注的时间
				Date bettingDate2 = winningResults.get(0).getBettingDate();
				SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
				String bettingTime = sdf1.format(bettingDate2);

				map.put("bettingrule", bettingrule);
				map.put("state", state);
				map.put("winningamount", get);
				map.put("bettingTime", bettingTime);

				list.add(map);
				// 有一个中奖状态为中奖
				if ((1 == state || 2 == state) && state1 < 1) {
					state1 = state;
				}
			}
			DecimalFormat df = new DecimalFormat("#.##");
			String getBet = df.format(betTotalAmount);
			String getWin = df.format(winTotalAmount);
			maxMap.put("betTotalAmount", getBet);
			maxMap.put("winTotalAmount", getWin);
			maxMap.put("totalCount", selectWinningResultsCount);
			maxMap.put("opencode", opencode);
			maxMap.put("state1", state1);
			maxMap.put("bettingDate", bettingDate);
			maxMap.put("listdata", list);
			maxMap.put("result", "0");
			maxMap.put("msg", "查询成功！");

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("orderDetails error:", e);
			maxMap.put("result", "1");
			maxMap.put("msg", "查询失败！");
		}

		return JSON.toJSONString(maxMap, true);
	}

}
